#include "GlobalChartOptions.h"
#include "../../common/utility.h"
#include <QMessageBox>

namespace LightChart
{
GlobalChartOptions::GlobalChartOptions(QWidget* parent) : QDialog(parent), settings(GlobalChartSettings::get_instance())
{
    setupUi(this);
    settings.chartTitle = std::string("Default global chart's title");
    settings.bgColor = QColor(240,240,240);
    settings.height = 300;
    settings.width = 500;
    lineEditTitle->setText(settings.chartTitle.c_str());
    lineEditWidth->setText(QString::number(settings.width));
    lineEditHeight->setText(QString::number(settings.height));
    bgColor->setStyleSheet(getBgColorStyleSheet(settings.bgColor));
    setupConnections();
}
void GlobalChartOptions::setupConnections()
{
    connect(okButton, SIGNAL(clicked()), this, SLOT(onOkClicked()));
    connect(bgColorButton, SIGNAL(clicked()), this, SLOT(onBgColorButtonClicked()));
}
void GlobalChartOptions::onOkClicked()
{
    try
    {
        settings.chartTitle = lineEditTitle->text().toStdString();
        settings.bgColor = tmpColor;
        if(!lineEditWidth->text().isEmpty())
        {
            double tmp = QStringToDouble(lineEditWidth->text());
            if(tmp < 100 || tmp > 1920) throw std::runtime_error("Wrong width specified. Width must be in [100 : 1920]");
            else settings.width = tmp;
        }
        if(!lineEditHeight->text().isEmpty())
        {
            double tmp = QStringToDouble(lineEditHeight->text());
            if(tmp < 50 || tmp > 1080) throw std::runtime_error("Wrong height specified. Height must be in [50 : 1080]");
            else settings.height = tmp;
        }
        hide();
    }
    catch(std::runtime_error& e)
    {
        e.what();
        QMessageBox::critical(this,"Input data corrupted.",e.what());
    }

}
void GlobalChartOptions::onBgColorButtonClicked()
{
    tmpColor = QColorDialog::getColor(Qt::white);
    bgColor->setStyleSheet(getBgColorStyleSheet(tmpColor));
}
}
